import axios from 'axios'
import { Toast } from 'vant'
import qs from 'qs'

axios.defaults.baseURL = 'http://www.520gongfu.com:9527/api'

// 添加请求拦截器
axios.interceptors.request.use(function(config) {

    // 设置 请求 token 校验
    const token = sessionStorage.getItem('token')
    if (token) {
        config.headers['Authorization'] = token
    }
    // 在发送请求之前做些什么
    return config;
}, function(error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

//获取轮播数据
export const getSliders = (callback) => {
    axios.get('/home/sliders').then(function(response) {
        callback && callback(response.data)
    }).catch(function(error) {
        console.log(error);
    })
}

//获取首页分类数据
export const getCategoryies = () => {
    return axios.get('/home/category').then(res => {
        const { data } = res
        if (data.status == 0) {
            return data.data
        } else {
            Toast.fail(data.msg)
        }
    })
}

//获取楼层分类数据
export const getFloors = () => {
    return axios.get('/home/floorlist').then(res => {
        const { data } = res
        if (data.status == 0) {
            return data.data
        } else {
            Toast.fail(data.msg)
        }
    })
}

// 获取 分类页面数据
export const getCategoryData = (callback) => {
    axios.get('/category/data').then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}

/**
 * 登录
 */
export const login = (params, callback) => {
    return axios.post('/user/login', qs.stringify(params)).then(res => {
        const { data } = res
        callback && callback(data)
    }).catch(err => {
        console.log(err);
    })
}

/**
 * 获取用户信息
 * */
export const getUserInfo = (callback) => {
    return axios.get('/user/info').then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}

/**
 * 获取 购物车数据
 * */
export const getCartList = (callback) => {
    return axios.get('/cart/list').then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })

}

/**
 * 删除购物车数据
 * */
export const deleteCartItem = (params, callback) => {
    return axios.post('/cart/del', qs.stringify(params)).then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}

/**
 * 添加购物车数据
 * */
export const cartAdd = (params, callback) => {
    return axios.post('/cart/add', qs.stringify(params)).then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}


/**
 * 推荐商品 
 * */

export const getRecommend = (callback) => {
    return axios.get('/product/recommend').then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}



//商品列表
export const getProductList = (params, callback) => {
    return axios.get('/product/list', {
        params: params,
        //序列化请求参数
        paramsSerializer: function(params) {
            return qs.stringify(params)
        }
    }).then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}


//获取商品详情
export const getProductDetail = (params) => {
    return axios.get('/product/detail', {
        params
    }).then(res => {
        const { data } = res
        if (data.status == 0) {
            return data.data
        } else {
            Toast.fail(data.msg)
        }
    })
}